//
//  InvoiceDetailViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "InvoiceDetailViewModel.h"
#import "WithDrawlRecordDetailTableViewCell.h"
#import "InvoiceDetailTableViewCell.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "InvoiceDetailHeaderView.h"
#import "InvoiceDetialModel.h"

@interface InvoiceDetailViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    NSMutableArray *leftArr;
    InvoiceDetialModel *detailModel;
    NSArray *dataArr;  // 拼接好的数据列表
}

@end

@implementation InvoiceDetailViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView registerClass:[InvoiceDetailHeaderView class] forHeaderFooterViewReuseIdentifier:@"InvoiceDetailHeaderView"];
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [superView addSubview:tableView];
        detailModel = [InvoiceDetialModel new];
    }
    return self;
}

- (void)setType:(MyInvoiceType)type
{
    _type = type;
    if (type == MyInvoiceType_WaitingFor) {
        leftArr = [NSMutableArray arrayWithObjects:@"订单号", @"下单时间", @"实际支付金额", @"开票状态", @"申请时间", nil];

    } else if (type == MyInvoiceType_Haved) {
        leftArr = [NSMutableArray arrayWithObjects:@"订单号", @"下单时间", @"实际支付金额", @"开票状态", @"申请时间", @"发票号码", @"物流公司", @"物流单号", nil];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(18);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footer;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    InvoiceDetailHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"InvoiceDetailHeaderView"];
    return headerV;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return leftArr.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (leftArr.count == indexPath.row) {
        InvoiceDetailTableViewCell *detail = [InvoiceDetailTableViewCell cellWithTableView:tableView];
        if (detailModel.data.objectUrl) {
            detail.imgStr = detailModel.data.objectUrl;
        }
        return detail;
    }
    WithDrawlRecordDetailTableViewCell *detailCell = [WithDrawlRecordDetailTableViewCell cellWithTableView:tableView];
    if (indexPath.row == 2) {
        detailCell.rightLab.textColor = [UIColor colorWithHexString:@"#EC5353"];
    } else if (indexPath.row == 3) {
        if (self.type == MyInvoiceType_Haved) {
            detailCell.rightLab.textColor = [UIColor colorWithHexString:@"#A89A60"];
        } else {
            detailCell.rightLab.textColor = [UIColor colorWithHexString:@"#EC5353"];
        }
    } else {
        detailCell.rightLab.textColor = kColor_Rule_Black;
        
    }
    if (indexPath.row == leftArr.count - 1 ) {
        [detailCell hiderLine:YES];
    } else {
        [detailCell hiderLine:NO];
    }
    detailCell.leftStr = leftArr[indexPath.row];
    if (dataArr.count > 0) {
        detailCell.rightLab.text = dataArr[indexPath.row];
    }
    return detailCell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.BlockWithDrawlDetail) {
        self.BlockWithDrawlDetail(indexPath, @"");
    }
}

#pragma mark- 发票详情接口
- (void)requestInvoiceDetail
{
    NSDictionary *params = @{@"id" : koString(_deModel.applyId)}; // 发票id
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyInvoiceListDetail] lodingView:nil lodingTextString:@"" isLoding:YES parameters:params success:^(id  _Nullable response) {
        [self makeDetailData:response];
    } failure:^(NSError * _Nullable error) {
    }];
}

- (void)makeDetailData:(id)respose
{
    detailModel = [InvoiceDetialModel yy_modelWithJSON:respose];
    if (detailModel.code == 0) {  // 返回成功 数据处理，整理成数组
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:koString(@"")];  // 订单号
        [arr addObject:koString(@"")];  // 下单时间
        [arr addObject:koString(@"")];  // 实际支付金额
        [arr addObject:koString(@"")];  // 实际支付状态
        [arr addObject:koString(@"")];  // 申请时间
        
        if (self.type == MyInvoiceType_Haved) {
            [arr addObject:koString(@"")]; // 发票号码
            [arr addObject:koString(@"")]; // 物流公司
            [arr addObject:koString(@"")]; // 物流单号
        }
        dataArr = [NSArray arrayWithArray:arr];
    }
}

@end
